﻿using System;
using System.Globalization;
using System.IO;
using System.Text;

namespace Edmx.Utility
{
    /// <summary>
    /// A <see cref="StringWriter"/> that supports encoding.
    /// </summary>
    public class StringEncodingWriter : StringWriter
    {
        private readonly Encoding _encoding;

        /// <summary>
        /// Initializes a new instance of the <see cref="StringEncodingWriter"/> class.
        /// </summary>
        public StringEncodingWriter()
            : this(new StringBuilder(), Encoding.Unicode, CultureInfo.CurrentCulture)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="StringEncodingWriter"/> class.
        /// </summary>
        /// <param name="encoding">The <see cref="Encoding"/> in which the output is written.</param>
        public StringEncodingWriter(Encoding encoding)
            : this(new StringBuilder(), encoding, CultureInfo.CurrentCulture)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="StringEncodingWriter"/> class.
        /// </summary>
        /// <param name="formatProvider">An <see cref="T:System.IFormatProvider"/> object that controls formatting.</param>
        public StringEncodingWriter(IFormatProvider formatProvider)
            : this(new StringBuilder(), Encoding.Unicode, formatProvider)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="StringEncodingWriter"/> class.
        /// </summary>
        /// <param name="sb">The <see cref="StringBuilder"/> to write to. </param>
        public StringEncodingWriter(StringBuilder sb)
            : this(sb, Encoding.Unicode, CultureInfo.CurrentCulture)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="StringEncodingWriter"/> class.
        /// </summary>
        /// <param name="sb">The <see cref="StringBuilder"/> to write to. </param>
        /// <param name="formatProvider">An <see cref="T:System.IFormatProvider"/> object that controls formatting.</param>
        public StringEncodingWriter(StringBuilder sb, IFormatProvider formatProvider)
            : this(sb, Encoding.Unicode, formatProvider)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="StringEncodingWriter"/> class.
        /// </summary>
        /// <param name="sb">The <see cref="StringBuilder"/> to write to. </param>
        /// <param name="encoding">The <see cref="Encoding"/> in which the output is written.</param>
        public StringEncodingWriter(StringBuilder sb, Encoding encoding)
            : this(sb, encoding, CultureInfo.CurrentCulture)
        {}

        /// <summary>
        /// Initializes a new instance of the <see cref="StringEncodingWriter"/> class.
        /// </summary>
        /// <param name="sb">The <see cref="StringBuilder"/> to write to. </param>
        /// <param name="encoding">The <see cref="Encoding"/> in which the output is written.</param>
        /// <param name="formatProvider">An <see cref="IFormatProvider"/> object that controls formatting.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// 	<paramref name="sb"/> is <c>null</c>.
        /// </exception>
        public StringEncodingWriter(StringBuilder sb, Encoding encoding, IFormatProvider formatProvider)
            : base(sb, formatProvider)
        {
            _encoding = encoding ?? Encoding.Unicode;
        }

        /// <summary>
        /// Gets the <see cref="T:System.Text.Encoding"/> in which the output is written.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// The Encoding in which the output is written.
        /// </returns>
        public override Encoding Encoding
        {
            get { return _encoding; }
        }
    }
}